﻿// $Id: TypeMapper.cs 68 2008-05-09 16:11:33Z nvivo $

using System;
using System.Collections.Generic;
using System.Text;

namespace DBLGen.Plugins.Batch
{
    [Plugin("Type Mapper")]
    [PluginSettings(typeof(TypeMapperSettings), typeof(TypeMapperSettingsEditor))]
    public class TypeMapper : BatchPlugin
    {
        public new TypeMapperSettings Settings
        {
            get
            {
                return (TypeMapperSettings) base.Settings;
            }
            set
            {
                base.Settings = value;
            }
        }

        public override void Process(Table table)
        {
            TypeMapping mapping = Settings.Mapping;

            if (mapping == null)
                throw new InvalidOperationException("There is no mapping defined.");

            bool useNullables = Settings.UseNullables;
            string ca = Settings.CustomAttribute;

            foreach (Column c in table.Columns)
            {
                string mappedType = mapping.GetMappedType(c.SqlType, useNullables);

                if (ca == null)
                    c.Type = mappedType;
                else
                    c.CustomAttributes[ca] = mappedType;
            }
        }
    }
}
